﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject;
using NTTDATA.BEENChina.ResultCertification.Result;
using NTTDATA.BEENChina.ResultCertification.Split; 

namespace NTTDATA.BEENChina.ResultCertification.Checker
{ 
    public class VerfityOneResult:Verfity
    {
        [Category("检查对象的JobRank"), Description("设置要检查的JobRank")]
        public EnumRank VerfityRanks
        {
            get { return _verfityRanks; }
            set { _verfityRanks = value; }
        }

        private EnumRank _verfityRanks = EnumRank.Final;

        private EnumRank _compareRank = EnumRank.Final;

        public EnumRank CompareRank
        {
            get { return _compareRank; }
            set { _compareRank = value; }
        }
        

        public override bool Check()
        {
            bool bResult = false;
             
           
            foreach (int i in Enum.GetValues(typeof(EnumRank)))
            {
                EnumRank rank = (EnumRank)i;

                if ((VerfityRanks & rank) == rank)
                {
                    string entryResult = string.Empty, entryCommon = string.Empty;

                    if( ChildImageObject.OperatorEntryResults.Count(t => t.Rank == rank) >0)
                    {
                        var resultObject = ChildImageObject.OperatorEntryResults.Single(t => t.Rank == rank);

                        entryResult = resultObject.EntryResult;
                        entryCommon = resultObject.EntryCommon;

                    } 

                    if(string.IsNullOrEmpty (entryResult+entryCommon))
                    {
                        continue;
                    }

                    Split.ISplit split = new SplitResult(ChildImageObject.CategorySetting);

                    List<List<string>> splitResult = split.GetSplitResult(entryResult).ToList();

                    //检验对象总字数
                    VerfityWordCount = splitResult.Sum(t => t.Sum(tt => tt.Replace("/",string.Empty).Length));

                    bResult = CheckResult(splitResult, entryCommon);
                    if (bResult)
                        break;
                }
            }
            
            return bResult;
        }

        public virtual bool CheckResult(List<List<string>> result, string entryCommon)
        {
            return false;
        }

        protected void SetErrorRange(string text)
        {
            ChildImageObject.Reason.ErrorRanage =
                string.Format("{0}{1}キーワード：{2}", Util.StrHelper.RankToString(CompareRank), Environment.NewLine, text);
        }

        #region 整个结果集和  记载率_未(有)记载

        /// <summary>
        /// 整个结果集和 记载率_未(有)记载
        /// </summary>
        /// <param name="result">分割成行列的结果集</param>
        /// <param name="entryCommon">结果集对应的付签</param>
        /// <param name="entryResultEmpty">有记载 无记载</param>
        /// <returns>布尔值</returns>
        protected bool CheckEmpty(List<List<string>> result, string entryCommon, bool entryResultEmpty)
        {
            string _comment = "30";
            bool bresult = entryResultEmpty
                               ? EntryResultIsEmpty(result) && string.CompareOrdinal(entryCommon , _comment).Equals(0)
                               : !EntryResultIsEmpty(result);
            return bresult;
        }

        protected bool EntryResultIsEmpty(List<List<string>> result)
        {
            bool bempty = false;
            bempty =  result != null && result.All(o => o != null && o.All(t => string.IsNullOrEmpty(t.Replace("/", string.Empty))));

            return bempty;
        }

        #endregion



        #region 第N行 (特定部分) 有/无记载

        /// <summary>
        /// 第N行 (特定部分) 有/无记载
        /// </summary>
        /// <param name="result">分割成行列的结果集</param>
        /// <param name="getSpeicficResult">得到结果中的特定部分的方法</param>
        /// <param name="exists">有记载，无记载</param>
        /// <param name="specificRow">特定行</param>
        /// <param name="specificCol">特定列</param>
        /// <returns>布尔值</returns>
        protected bool CheckSpecificRowIsEmpty(List<List<string>> result,  Func<string, string> getSpeicficResult, bool exists, int specificRow,int specificCol = -1)
        {
            bool bresult = false;
            if (exists)
            {
                //判断有记录
                if (specificRow  < result.Count && specificRow  >= 0)
                {
                    //row >0 col =-1
                    if (specificCol == -1)
                    {
                        //判断第N行结果中 特定部分 是否有记载 
                        //bresult = result[specificRow - 1].Any(colStr =>
                        bresult = result[specificRow ].Any(colStr =>
                            {
                                string specificResult = getSpeicficResult(colStr);
                                return !string.IsNullOrEmpty(specificResult);
                            });
                    }
                    else
                    {
                        //row>0 col>0
                        //判断第N行N 行 结果中 特定部分 是否有记载 
                        //string cellResult = result[specificRow - 1][specificCol];
                        string cellResult = result[specificRow][specificCol];
                        string specificResult = getSpeicficResult(cellResult);
                        return !string.IsNullOrEmpty(specificResult);
                         
                    }
                }
                else //row = -1
                {
                    //row = -1 col =-1
                    if (specificCol == -1 && specificRow == -1)
                    {
                        //判断 任意位置 结果中 特定部分 是否有空白 
                        bresult = result.Any(row => row.Any(colStr =>
                        {
                            string specificResult = getSpeicficResult(colStr);
                            return !string.IsNullOrEmpty(specificResult);
                        }));

                    }
                    else if (specificCol > -1 && specificRow == -1)
                    {
                        //row =-1 col>0
                        int colCount = result[0].Count;
                        if (specificCol < colCount)
                        {
                            //判断第N列 结果中 特定部分 是否有记载 
                            var coll = result.Select(t => t[specificCol]).ToList();

                            bresult = coll.Any(cellResult =>
                            {
                                string specificResult = getSpeicficResult(cellResult);
                                return !string.IsNullOrEmpty(specificResult);
                            });
                        }


                    }

                }
            }
            else
            {
                //判断未记载
                if (specificRow  >= result.Count )
                {
                    bresult = true;
                } 
                else if (specificRow < result.Count && specificRow >= 0)
                {
                    if (specificCol == -1)
                    {
                        //判断第N行结果中 特定部分 是否有空白 
                        //bresult = result[specificRow - 1].Any(colStr =>
                        bresult = result[specificRow].Any(colStr =>
                        {

                            string specificResult = getSpeicficResult(colStr);
                            return string.IsNullOrEmpty(specificResult);
                        });
                    }
                    else
                    {
                        //判断第N行N 行 结果中 特定部分 是否有记载 
                        //string cellResult = result[specificRow - 1][specificCol];
                        string cellResult = result[specificRow][specificCol];
                        string specificResult = getSpeicficResult(cellResult);
                        return string.IsNullOrEmpty(specificResult);
                    }
                }
                else //row = -1
                {
                    if (specificCol == -1 && specificRow == -1)
                    {
                        //判断 任意位置 结果中 特定部分 是否有空白 
                        bresult = result.Any(row=>row.Any(colStr =>
                        { 
                            string specificResult = getSpeicficResult(colStr);
                            return string.IsNullOrEmpty(specificResult);
                        }));
                       
                    }
                    else if (specificCol > -1 && specificRow == -1)
                    {
                        int colCount = result[0].Count;
                        if (specificCol < colCount)
                        { 
                             //判断第N列 结果中 特定部分 是否有记载 
                            var coll = result.Select(t => t[specificCol]).ToList();

                            bresult = coll.Any(cellResult =>
                                                   {
                                                       string specificResult = getSpeicficResult(cellResult);
                                                       return string.IsNullOrEmpty(specificResult);
                                                   });
                        }
                       
                       
                    }

                }
            }
            return bresult;
        }


        #endregion


        #region 录入结果中特定部分 包含特定字符

        /// <summary>
        /// 检查结果集中指定部分
        /// </summary>
        /// <param name="result">分割成行列的结果集</param>
        /// <param name="getSpeicficResult">得到结果中的特定部分的方法</param>
        /// <param name="checkspecificResult">检查特定部分的方法</param>
        /// <returns>布尔值</returns>
        protected bool CheckSpecificCharacter(List<List<string>> result, Func<string, string> getSpeicficResult, Func<string, bool> checkspecificResult)
        {
            bool bresult = false;

            foreach (var rowResult in result)
            {
                foreach (string colResult in rowResult)
                {

                    string specificResult = getSpeicficResult(colResult);

                    if (checkspecificResult(specificResult))
                        bresult = true;

                    if (bresult)
                        break;
                }
            }
            return bresult;
        }

        /// <summary>
        /// 检查结果集中指定行列部分
        /// </summary>
        /// <param name="result">分割成行列的结果集</param>
        /// <param name="getSpeicficResult">得到结果中的特定部分的方法</param>
        /// <param name="specialStringArray">检查特定部分的方法</param>
        /// <returns>布尔值</returns>
        protected bool CheckSpecificCharacter(List<List<string>> result, Func<string, string> getSpeicficResult, SpecialStringArray specialStringArray)
        {
            bool bresult = false;
            int row = 0, col = 0;
            foreach (var rowResult in result)
            {

                foreach (string colResult in rowResult)
                {
                    string specificResult = getSpeicficResult(colResult);

                    //if (checkspecificResult(row, col, specificResult))
                    if (specialStringArray.Check(row, col, specificResult))
                        bresult = true;

                    if (bresult)
                        break;

                    col++;

                }

                if (bresult)
                    break;

                row++;
            }
            return bresult;
        }
      /*  protected bool CheckSpecificCharacter(List<List<string>> result, Func<string, string> getSpeicficResult, Func<int,int,string, bool> checkspecificResult)
        {
            bool bresult = false;
            int row =0, col =0;
            foreach (var rowResult in result)
            {
                
                foreach (string colResult in rowResult)
                {
                    string specificResult = getSpeicficResult(colResult);

                    if (checkspecificResult(row,col,specificResult))
                        bresult = true;

                    if (bresult)
                        break;

                    col++;

                }
                row ++;
            }
            return bresult;
        }*/

        /// <summary>
        /// 检查结果集中指定部分
        /// </summary>
        /// <param name="result">分割成行列的结果集</param> 
        /// <param name="checkResult">检查特定部分的方法</param>
        /// <returns>布尔值</returns>
        protected bool CheckSpecificCharacter(List<List<string>> result,  Func<string, bool> checkResult)
        {
            bool bresult = false;

            foreach (var rowResult in result)
            {
                foreach (string colResult in rowResult)
                {
                    if ( checkResult(colResult))
                        bresult = true;

                    if (bresult)
                        break;
                }
            }
            return bresult;
        }
        #endregion

        #region 补全日期

        /// <summary>
        /// 补全日期
        /// 年月日缺失的 补充为当前时间对应部分
        /// 用于比较字符序列
        /// </summary>
        /// <param name="entryResults"></param>
        /// <returns></returns>
        protected List<string> CompleteDate(List<string> entryResults)
        {
            if (entryResults == null)
            {
                return null;
            }

            List<string[]> dateList = entryResults.Where(o => o != null).Select<string, string[]>(o => { return o.Split(new char[] { '/' }); }).ToList();
            string[] date = new string[4];  //0:gg;1:yy;2:mm;3:dd
            for (int i = 0; i < dateList.Count(); i++)
            {
                //年号，年月日全空的，不补
                if (dateList[i].All(string.IsNullOrEmpty))
                {
                    continue;
                }
                //i= 0:gg,1:yy,2:mm,3:dd
                for (int j = 0; j < 3; j++)
                {
                    //如果当前项目不为空，则保留。date中的内容是截止当前行最新最全的日期。
                    date[j] = dateList.Count() > j && !string.IsNullOrEmpty(dateList[i][j]) ? dateList[i][j] : date[j];
                    //如果对应的日期部分为空并且date中的内容不为空，则替换。
                    dateList[i][j] = string.IsNullOrEmpty(dateList[i][j]) && !string.IsNullOrEmpty(date[j]) ? date[j] : dateList[i][j];
                }
            }

            return dateList.Select<string[], string>(o => string.Join("/", o)).ToList();
        }



        /// <summary>
        /// 补充为 01
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        protected string  CompleteDate(string date)
        {
            if (String.IsNullOrEmpty(date.Replace("/", string.Empty)))
                return string.Empty;
                
            var dateHelper = new DateHelper(ChildImageObject.CategorySetting);

            //公历变换和历
            date = dateHelper.ChangeDateStr(date);

            //年月日
            date = dateHelper.CompleteDate(date);

            return date;
        }
        #endregion
        
    }
}
